﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QqForm
{
    public partial class ChatForm : Form
    {
        private TcpClass tcpClass;
        private string _uid;
        private string _nk;

        public ChatForm()
        {
            InitializeComponent();
            BindBrowser();
        }

        public ChatForm(TcpClass tcp, string nk, string uid)
        {
            InitializeComponent();
            this.tcpClass = tcp;
            this.Uid = uid;
            this.Nk = nk;
            this.Text = string.Format(@"与{0}({1})聊天中...", nk, uid);
            BindBrowser();
        }

        public ChatForm(TcpClass tcp, string nk, string uid, string msg, DateTime dateTime)
        {
            InitializeComponent();
            this.tcpClass = tcp;
            this.Uid = uid;
            this.Nk = nk;
            this.Text = string.Format(@"与{0}({1})聊天中...", nk, uid);
            rtb_GetInfo.Text += string.Format("对方 {0},\r\n{1} \r\n", dateTime.ToString(), msg);
            BindBrowser();
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        public string Uid
        {
            get { return _uid; }
            set { _uid = value; }
        }

        public string Nk
        {
            get { return _nk; }
            set { _nk = value; }
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            SendMsg();
        }

        private void SendMsg()
        {
            QqCommand qqCommand = new QqCommand();
            qqCommand.SendMsgCommand(tcpClass, Uid, rtb_sendInfo.Text);
            rtb_GetInfo.AppendText(string.Format("我 {0},\r\n{1} \r\n", DateTime.Now, rtb_sendInfo.Text));
            rtb_sendInfo.Text = "";
        }

        public void Shows(string msg, DateTime time)
        {
            this.Show();
            this.rtb_GetInfo.AppendText(string.Format("对方 {0} \r\n {1} \r\n", time.ToString(), msg));
        }

        public void MsgHelper(string msg, DateTime time)
        {
            this.rtb_GetInfo.AppendText(string.Format("对方 {0} \r\n {1} \r\n", time.ToString(), msg));

        }

        #region 关闭当前会话
        /// <summary>
        /// 关闭当前会话
        /// </summary>
        private void CloseCurrentSessions()
        {
            for (int i = 0; i < QqUserHelper.ChatFormHelpers.Count; i++)
            {
                if (QqUserHelper.ChatFormHelpers[i].Uid == this._uid)
                {
                    QqUserHelper.ChatFormHelpers.RemoveAt(i);
                    this.Close();
                }
            }
        }
        #endregion

        private void MinimizeCurrentSessions()
        {
            foreach (ChatFormHelper item in QqUserHelper.ChatFormHelpers)
            {
                if (item.Uid == this._uid)
                {
                    item.State = 1;
                    this.Hide();
                    return;
                }
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            CloseCurrentSessions();
        }

        private void ChatForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseCurrentSessions();
        }

        private void ChatForm_Load(object sender, EventArgs e)
        {

        }

        private void BindBrowser()
        {
            //webBrowser1.Url = new System.Uri(Application.StartupPath + "\\ExeDll\\kindeditor\\e.html", System.UriKind.Absolute);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}
